package com.weds.edwx.web;

import com.alibaba.fastjson.JSONObject;
import com.weds.core.utils.DateUtils;
import com.weds.core.utils.StringUtils;
import com.weds.edpf.core.entity.AcDepEntity;
import com.weds.edpf.core.service.AcDepService;
import com.weds.edwx.entity.EdDineMealSettingEntity;
import com.weds.edwx.entity.EdDineOrderEntity;
import com.weds.edwx.entity.EdDineOrderInfoEntity;
import com.weds.edwx.service.EdDineMealSettingService;
import com.weds.edwx.service.EdDineOrderInfoService;
import com.weds.edwx.service.EdDineOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.validation.Valid;

import com.weds.core.base.BaseController;
import com.weds.core.annotation.Logs;
import com.weds.core.resp.JsonResult;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BasePageSearch;

import com.weds.edwx.entity.EdDineOrderDetailEntity;
import com.weds.edwx.service.EdDineOrderDetailService;

/**
 * @Author
 * @Description 订餐订单详情管理
 * @Date 2019-10-22
 */
@RestController
@RequestMapping(value = "/edDineOrderDetail")
@Api(value = "订餐订单详情管理", description = "订餐订单详情管理")
public class EdDineOrderDetailController extends BaseController {
    @Resource
    private EdDineOrderDetailService edDineOrderDetailService;

    @Autowired
    private AcDepService acDepService;

    @Autowired
    private EdDineMealSettingService edDineMealSettingService;

    @Autowired
    private EdDineOrderService edDineOrderService;

    @Autowired
    private EdDineOrderInfoService edDineOrderInfoService;

    private Logger log = LogManager.getLogger();

    @Logs
    @ApiOperation(value = "新增订餐订单详情信息", notes = "新增订餐订单详情信息")
    @RequestMapping(value = "/insert", method = RequestMethod.PUT)
    public JsonResult<Object> insert(@RequestBody @Valid EdDineOrderDetailEntity record) {
        Date date = new Date();
        record.setCtDate(date);
        record.setLtDate(date);
        edDineOrderDetailService.insertSelective(record);
        return succMsg();
    }

    @Logs
    @ApiOperation(value = "终端完成取餐", notes = "终端完成取餐")
    @RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST)
    public JsonResult<Object> updateByPrimaryKey(@RequestBody @Valid EdDineOrderInfoEntity record) {
        record.setMealState("3");
        record.setLtDate(new Date());
        record.setTakeUser("3");//取餐人
        edDineOrderInfoService.updateByPrimaryKeySelective(record);
        return succMsg();
    }

    @Logs
    @ApiOperation(value = "查询订餐订单详情清单", notes = "查询订餐订单详情清单")
    @RequestMapping(value = "/selectListPageByEntity", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<EdDineOrderDetailEntity>> selectListPageByEntity(@RequestBody BasePageSearch<EdDineOrderDetailEntity> record) {
        setPageHelper(record);
        EdDineOrderDetailEntity entity = record.getSearch();
        if (entity == null) {
            entity = new EdDineOrderDetailEntity();
        }
        List<EdDineOrderDetailEntity> list = edDineOrderDetailService.selectListByEntity(entity);
        return succMsgData(new BaseCommPager<EdDineOrderDetailEntity>(list));
    }


    @Logs
    @ApiOperation(value = "查询餐厅订餐汇总列表", notes = "查询餐厅订餐汇总列表")
    @RequestMapping(value = "/selectFoodCollectList", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<EdDineOrderDetailEntity>> selectFoodCollectList(@RequestBody BasePageSearch<EdDineOrderEntity> record) {
        EdDineOrderEntity entity2 = record.getSearch();
        AcDepEntity dep = acDepService.selectByPrimaryKey(Integer.parseInt(entity2.getPlaceId()));
        if(dep == null){
            return message("602", "当前餐厅不存在");
        }
        setPageHelper(record);
        EdDineOrderEntity entity = record.getSearch();
        if (entity == null) {
            entity = new EdDineOrderEntity();
        }
        if(dep.getDepDefine() == 1){ //如果是窗口
            entity.setPlaceId(dep.getDepParent().toString());
            entity.setMerchantId(dep.getDepSerial());
        }
        List<EdDineOrderDetailEntity> list = edDineOrderDetailService.selectFoodCollectList(entity);
        return succMsgData(new BaseCommPager<EdDineOrderDetailEntity>(list));
    }

    @Logs
    @ApiOperation(value = "删除订餐订单详情信息", notes = "删除订餐订单详情信息")
    @RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.DELETE)
    public JsonResult<Object> deleteByPrimaryKey(@RequestParam String id) {
        edDineOrderDetailService.deleteByPrimaryKey(id);
        return succMsg();
    }

    @Logs
    @ApiOperation(value = "取餐时查询未取餐订单信息", notes = "取餐时查询未取餐订单信息")
    @RequestMapping(value = "/selectOrderInfo", method = RequestMethod.POST)
    public JsonResult<List<EdDineOrderEntity>> selectByPrimaryKey(@RequestBody EdDineOrderEntity record) {
        AcDepEntity dep = acDepService.selectByPrimaryKey(Integer.parseInt(record.getPlaceId()));
        if(dep == null){
            return message("602", "当前餐厅不存在");
        }
        if(dep.getDepDefine() == 1){ //如果是窗口
            record.setPlaceId(dep.getDepParent().toString());
            record.setMerchantId(dep.getDepSerial());
        }
        //查出当前时段对应的餐别
        EdDineMealSettingEntity record1 = new EdDineMealSettingEntity();
        record1.setPlaceId(record.getPlaceId());
        record1.setRepastStart(DateUtils.getCurrentDateTime("HH:mm"));
        EdDineMealSettingEntity edDineMealSettingEntity = edDineMealSettingService.selectNowMeal(record1);
        if(edDineMealSettingEntity == null){
            return message("601", "当前无餐别");
        }
        //查出对应餐别对应的订餐列表
        record.setMealId(edDineMealSettingEntity.getMealId());
        record.setOrderDate(DateUtils.getCurrentDate());
        if(!StringUtils.isBlank(record.getUserSerial())){
            record.setOrderState("1"); //只查出来已支付未取餐的订单
        }
        //该方法同时兼容根据人员序号和订单号查询
        List<EdDineOrderEntity> entityList = edDineOrderService.selectOrderListByUser(record);

        if(entityList == null || entityList.size() == 0){
            return message("601", "当前餐别无订餐信息");
        }
        for(EdDineOrderEntity edDineOrderEntity : entityList){
            edDineOrderEntity.setFoodList(edDineOrderDetailService.selectFoodDetailList(edDineOrderEntity));
        }
        return succMsgData(entityList);
    }


    @Logs
    @ApiOperation(value = "批量删除订餐订单详情信息", notes = "批量删除订餐订单详情信息")
    @RequestMapping(value = "/deleteBatchByKeys", method = RequestMethod.POST)
    public JsonResult<Object> deleteBatchByKeys(@RequestBody List<EdDineOrderDetailEntity> list) {
        List<EdDineOrderDetailEntity> delList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            delList.add(list.get(i));
            if (delList.size() == 20 || i == list.size() - 1) {
                edDineOrderDetailService.deleteBatchByKeys(delList);
                delList.clear();
            }
        }
        return succMsg();
    }

    @Logs
    @ApiOperation(value = "根据设备查询餐厅或窗口信息", notes = "根据设备查询餐厅或窗口信息")
    @RequestMapping(value = "/selectDineHallInfo", method = RequestMethod.GET)
    public JsonResult<AcDepEntity> selectDineHall(@RequestParam String devSbId) {
        //先根据传进来的设备ID devSbId
        AcDepEntity acDepEntity  = acDepService.selectDineHall(devSbId);
        if(acDepEntity == null){
            return message("601", "该设备无绑定的窗口或者餐厅");
        }
        return succMsgData(acDepEntity);
    }

    @Logs
    @ApiOperation(value = "根据订单号查询订餐信息", notes = "根据订单号查询订餐信息")
    @RequestMapping(value = "/selectOrderInfoByOrderInd", method = RequestMethod.POST)
    public JsonResult<EdDineOrderEntity> selectOrderInfoByOrderId(@RequestBody EdDineOrderEntity record) {
        AcDepEntity dep = acDepService.selectByPrimaryKey(Integer.parseInt(record.getPlaceId()));
        if(dep == null){
            return message("605", "当前餐厅不存在");
        }
        if(dep.getDepDefine() == 1){ //如果是窗口
            record.setPlaceId(dep.getDepParent().toString());
            record.setMerchantId(dep.getDepSerial());
        }
        //查出当前时段对应的餐别
        EdDineMealSettingEntity record1 = new EdDineMealSettingEntity();
        record1.setPlaceId(record.getPlaceId());
        record1.setRepastStart(DateUtils.getCurrentDateTime("HH:mm"));
        EdDineMealSettingEntity edDineMealSettingEntity = edDineMealSettingService.selectNowMeal(record1);

        //通过订单号查询订单详细
       EdDineOrderEntity orderInfo = edDineOrderService.selectOrderListByOrderId(record);

        if(orderInfo == null){
            return message("601", "订单号有误，请重新输入");
        }
        if((dep.getDepDefine() == 1 && !orderInfo.getMerchantId().equals(record.getMerchantId()))){
            return message("602","非取餐窗口，无法取餐",orderInfo); //不是当前餐厅的订单
        }
        if(dep.getDepDefine() == 0 && !orderInfo.getPlaceId().equals(record.getPlaceId())){
            return message("602","非取餐窗口，无法取餐",orderInfo); //不是当前餐厅的订单
        }
        if(!orderInfo.getOrderDate().equals(DateUtils.getCurrentDate())){
            return message("603","非取餐时间，无法取餐",orderInfo); //不是今天的订单
        }
        if(edDineMealSettingEntity == null || !orderInfo.getMealId().equals(edDineMealSettingEntity.getMealId())){
            return message("604","非取餐时间，无法取餐",orderInfo); //不是当前餐别的餐
        }
        orderInfo.setFoodList(edDineOrderDetailService.selectFoodDetailList(orderInfo));
        return succMsgData(orderInfo);
    }

}